package com.ruoyi.labdata.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.domain.LabPlatformInfo;
import com.ruoyi.labdata.domain.LabPlatformSubordinate;
import com.ruoyi.labdata.mapper.LabOrgnizationMapper;
import com.ruoyi.labdata.mapper.LabPlatformInfoMapper;
import com.ruoyi.labdata.service.ILabPlatformInfoService;
import com.ruoyi.labdata.service.ILabPlatformSubordinateService;

/**
 * 实验平台信息Service业务层处理
 * 
 * @author winner pan
 * @date 2022-04-26
 */
@Service
public class LabPlatformInfoServiceImpl implements ILabPlatformInfoService {
	@Autowired
	private LabPlatformInfoMapper labPlatformInfoMapper;
	@Autowired
	private LabOrgnizationMapper labOrgnizationMapper;
	@Autowired
	private ILabPlatformSubordinateService labPlatformSubordinateService;

	/**
	 * 查询实验平台信息
	 * 
	 * @param platformId
	 *            实验平台信息ID
	 * @return 实验平台信息
	 */
	@Override
	public LabPlatformInfo selectLabPlatformInfoById(Long platformId) {
		LabPlatformInfo labPlatformInfo = labPlatformInfoMapper.selectLabPlatformInfoById(platformId);
		if (labPlatformInfo != null) {
			LabPlatformSubordinate query = new LabPlatformSubordinate();
			query.setPlatformId(platformId);
			List<LabPlatformSubordinate> subordinateList = this.labPlatformSubordinateService
					.selectLabPlatformSubordinateList(query);
			List<Long> orgIdList = subordinateList.stream().map(s -> s.getOrgId()).collect(Collectors.toList());
			labPlatformInfo.setSubordinateList(orgIdList.stream().toArray(Long[] :: new));
		}
		return labPlatformInfo;
	}

	/**
	 * 查询实验平台信息列表
	 * 
	 * @param labPlatformInfo
	 *            实验平台信息
	 * @return 实验平台信息
	 */
	@Override
	public List<LabPlatformInfo> selectLabPlatformInfoList(LabPlatformInfo labPlatformInfo) {
		return labPlatformInfoMapper.selectLabPlatformInfoList(labPlatformInfo);
	}

	/**
	 * 新增实验平台信息
	 * 
	 * @param labPlatformInfo
	 *            实验平台信息
	 * @return 结果
	 */
	@Override
	public int insertLabPlatformInfo(LabPlatformInfo labPlatformInfo) {
		//因为组织机构表的ID和平台数据的ID在一个树上，ID不能重合，所以平台的ID需要使用机构表的ID.
		LabOrgnization labOrgnization = new LabOrgnization();
		labOrgnization.setOrgName("platform");
		labOrgnization.setOrgCode("platform");
		labOrgnization.setOrgGroupOpt(Long.valueOf(2));
		labOrgnization.setOrgFunctionOpt(Long.valueOf(1));
		labOrgnization.setParentId(Long.valueOf(0));
		labOrgnization.setCreateYear("2022");
		labOrgnization.setHasChildOpt(Long.valueOf(1));
		labOrgnization.setOrgStatusOpt(Long.valueOf(0));
		this.labOrgnizationMapper.insertLabOrgnization(labOrgnization);
		labPlatformInfo.setPlatformId(labOrgnization.getOrgId());
		labPlatformInfo.setCreateBy(SecurityUtils.getUsername());
		labPlatformInfo.setCreateTime(new Date());
		int ret = labPlatformInfoMapper.insertLabPlatformInfo(labPlatformInfo);
		if (labPlatformInfo.getSubordinateList() != null) {
			for (int i = 0; i < labPlatformInfo.getSubordinateList().length; i++) {
				LabPlatformSubordinate subordinate = new LabPlatformSubordinate();
				subordinate.setOrgId(labPlatformInfo.getSubordinateList()[i]);
				subordinate.setPlatformId(labPlatformInfo.getPlatformId());
				this.labPlatformSubordinateService.insertLabPlatformSubordinate(subordinate);
			}
		}
		this.labOrgnizationMapper.deleteLabOrgnizationById(labOrgnization.getOrgId());
		return ret;
	}

	/**
	 * 修改实验平台信息
	 * 
	 * @param labPlatformInfo
	 *            实验平台信息
	 * @return 结果
	 */
	@Override
	public int updateLabPlatformInfo(LabPlatformInfo labPlatformInfo) {
		LabPlatformSubordinate query = new LabPlatformSubordinate();
		query.setPlatformId(labPlatformInfo.getPlatformId());
		List<LabPlatformSubordinate> oldSubordinareList = this.labPlatformSubordinateService
				.selectLabPlatformSubordinateList(query);
		if (labPlatformInfo.getSubordinateList() == null) {
			List<Long> ids = oldSubordinareList.stream().map(item -> item.getSubordinateId())
					.collect(Collectors.toList());
			Long[] idArray = new Long[ids.size()];
			for(int i=0;i<ids.size();i++){
				idArray[i] = ids.get(i);
			}
			this.labPlatformSubordinateService.deleteLabPlatformSubordinateByIds(idArray);
		} else {
			for (Long orgId : labPlatformInfo.getSubordinateList()) {
				boolean isContain = false;
				for (LabPlatformSubordinate oldSubordinate : oldSubordinareList) {
					if (oldSubordinate.getOrgId().longValue() == orgId.longValue()) {
						oldSubordinareList.remove(oldSubordinate);
						isContain = true;
					}
				}
				if (!isContain) {
					LabPlatformSubordinate subordinate = new LabPlatformSubordinate();
					subordinate.setOrgId(orgId);
					subordinate.setPlatformId(labPlatformInfo.getPlatformId());
					this.labPlatformSubordinateService.insertLabPlatformSubordinate(subordinate);
				}
			}
			if (oldSubordinareList.size() > 0) {
				List<Long> ids = oldSubordinareList.stream().map(item -> item.getSubordinateId())
						.collect(Collectors.toList());
				this.labPlatformSubordinateService.deleteLabPlatformSubordinateByIds((ids.stream().toArray(Long[] :: new)));
			}

		}
		return labPlatformInfoMapper.updateLabPlatformInfo(labPlatformInfo);
	}

	/**
	 * 批量删除实验平台信息
	 * 
	 * @param platformIds
	 *            需要删除的实验平台信息ID
	 * @return 结果
	 */
	@Override
	public int deleteLabPlatformInfoByIds(Long[] platformIds) {
		return labPlatformInfoMapper.deleteLabPlatformInfoByIds(platformIds);
	}

	/**
	 * 删除实验平台信息信息
	 * 
	 * @param platformId
	 *            实验平台信息ID
	 * @return 结果
	 */
	@Override
	public int deleteLabPlatformInfoById(Long platformId) {
		return labPlatformInfoMapper.deleteLabPlatformInfoById(platformId);
	}
}
